<?php
/**
 * Semester Course Controller
 * Semester Course Information Management
 *
 * semester entry
 *
 * @version $Id: CourseController.php 126 2010-06-27 04:01:38Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package semester
 * @subpackage Controller
 */


class semester_CourseController extends Zend_Controller_Action
{
    function indexAction()
    {
        $this->_redirect('semester/course/set');
    }

    /**
     * 設定畫面
     *
     */
    function setAction ()
    {
        //學年學期
        if (!$this->_request->getParam('year_semester'))
        $this->view->year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
        else
        $this->view->year_semester = $this->_request->getParam('year_semester');
        //年級
        if (!$this->view->sel_grade = $this->_request->getParam('sel_grade'))
        $this->view->sel_grade = $this->view->lowYear;  // 預設最低學年

        // 預設領域
        $semester_course = new Semester_Model_SemesterCourse();
        $all_arr = $semester_course->getScoreName(Semester_Model_SemesterCourse::NO_RECURSIVE);
        foreach($all_arr as $i=>$row) {
            foreach($row as $row2)
            $grade_arr[$i][$row2['scope']] = $row2['scope'];
        }
        $this->view->semesterCousrse = Zend_Json::encode($grade_arr);


    }

    /**
     * 修改課程設定
     *
     */
    function editAction()
    {
        $arr = explode("-",$this->_request->getParam('year_semester'));
        $year = (int) $arr[0];
        $semester = (int) $arr[1];
        $grade = $this->_request->getParam('sel_grade',0);
        if ($this->_request->getParam('special') == 'Y')
        $special = 'Y';
        else
        $special = 'N';

        $rank = $this->_request->getParam('rank',0);
        $scope = $this->_request->getParam('scope');
        $name = $this->_request->getParam('subject_name');
        $scopeRank = $this->_request->getParam('scope_rank',0);

        if ($sn = $this->_request->getParam('del_sn',0)) { // 刪除
            // 先判斷是否有配課資料
            $q =  Doctrine_Query::create()
            ->select('COUNT(*) AS cc')
            ->from('Semester_Model_SemesterTeacherCourse a')
            ->leftJoin('a.Semester_Model_SemesterCourse b')
            ->where ('b.sn=?',$sn);
            $res = $q->fetchOne();
            if ($res->cc >0) {
                $this->_helper->json(0);
            }
            else {
                $q = Doctrine_Query::create()
                ->delete('Semester_Model_SemesterCourse')
                ->where('sn=?',$sn)
                ->andWhere('required=?',0);
                $q->execute();
            }
        }
        else {
            if ($sn = $this->_request->getParam('edit_sn')) { // 修改模式
                if ($scopeRank) { // 更動群組排序
                    $q = Doctrine_Query::create()
                    ->select('scope')
                    ->from('Semester_Model_SemesterCourse')
                    ->where('sn=?',$sn);
                    $row = $q->fetchOne();
                    $scopeArr = explode("-",$row->scope);
                    $q = Doctrine_Query::create()
                    ->update('Semester_Model_SemesterCourse')
                    ->set('scope_rank','?',$scopeRank)
                    ->where('scope_rank<>?', $scopeRank)
                    ->andWhere('year=?',$year)
                    ->andWhere('semester=?',$semester)
                    ->andWhere('grade=?',$grade)
                    ->andWhere("scope LIKE ?","{$scopeArr[0]}%");
                    $q->execute();
                }
                // 更新特殊學習科目
                $q = Doctrine_Query::create()
                ->update('Semester_Model_SemesterCourse')
                ->set('special','?',$special)
                ->set('name','?', $name)
                ->set('rank','?',$rank)
                ->where('sn=?',$this->getRequest()->getParam('edit_sn'));
                $q->execute();
            }
            else { // 新增模式
                $semesterCourse = new Semester_Model_SemesterCourse();
                $semesterCourse->scope = $scope;
                $semesterCourse->scope_rank = $scopeRank;
                $semesterCourse->year = $year ;
                $semesterCourse->semester = $semester;
                $semesterCourse->grade = $grade ;
                $semesterCourse->rank = $rank;
                $semesterCourse->name = $name;
                $semesterCourse->special = $special;
                $semesterCourse->save();
            }
        }
        $this->_helper->json('ok');
    }

    /**
     * 取得目前的課程
     *
     */
    function getsetAction()
    {
        $arr = explode("-",$this->_request->getParam('year_semester'));
        $year = (int)$arr[0];
        $semester = (int)$arr[1];
        $grade = (int)$this->_request->getParam('grade');
        $semester_course = new Semester_Model_SemesterCourse();
        $arr = $semester_course->getSemesterSubject($year,$semester,$grade);


        $semester_course = new Semester_Model_SemesterCourse();

        $this->_helper->json($arr);
    }

    /**
     * 取學年學期某一年級課程設定
     *
     */
    function getsemedataAction()
    {
        $arr = explode("-",$this->_request->getParam('year_semester'));
        $year = (int)$arr[0];
        $semester = (int)$arr[1];
        $grade = (int)$this->_request->getParam('grade');
        $q = Doctrine_Query::create()
        ->select('sn, year, semester, grade')
        ->from ('Semester_Model_SemesterCourse')
        ->groupBy('year, semester, grade')
        ->orderBy('year DESC, semester DESC');
        $res = $q->fetchArray();
        $this->_helper->json($res);
    }

    /**
     * 複製課程
     *
     */
    function copycourseAction()
    {
        $arr = explode("-",$this->_request->getParam('year_semester'));
        $year = (int)$arr[0];
        $semester = (int)$arr[1];
        $grade = (int)$this->_request->getParam('grade');
        $source_sn = (int)$this->_request->getParam('source_sn');
        $q = Doctrine_Query::create()
        ->select('sn')
        ->from('Semester_Model_SemesterCourse')
        ->where('year=?', $year)
        ->andWhere('semester=?', $semester)
        ->andWhere('grade=?', $grade);
        $count = $q->execute()->count();

        if ($count == 0 ) {
            // 課程複製
            $res = Doctrine_core::getTable('Semester_Model_SemesterCourse')->findOneBySn($source_sn)->toArray();
            $q = Doctrine_Query::create()
            ->from('Semester_Model_SemesterCourse')
            ->where('year=?', $res['year'])
            ->andWhere('semester=?', $res['semester'])
            ->andWhere('grade=?', $res['grade']);
            $row = $q->fetchArray() ;

            foreach($row as $val) {
                $semesterCourse = new Semester_Model_SemesterCourse();
                $semesterCourse->year= $year;
                $semesterCourse->semester =$semester;
                $semesterCourse->grade = $grade;
                $semesterCourse->scope = $val['scope'];
                $semesterCourse->name = $val['name'];
                $semesterCourse->scope_rank = $val['scope_rank'];
                $semesterCourse->rank = $val['rank'];
                $semesterCourse->required = $val['required'];
                $semesterCourse->save();
                $sn = $semesterCourse->sn;

                // 科目複製
                $q = Doctrine_Query::create()
                ->from('Semester_Model_SemesterSubject')
                ->where('subject_sn=?', $val['sn']);
                $row2 = $q->fetchArray();

                foreach($row2 as $val2){
                    $semesterSubject = new Semester_Model_SemesterSubject();
                    $semesterSubject->year= $year;
                    $semesterSubject->semester =$semester;
                    $semesterSubject->grade = $grade;
                    $semesterSubject->subject_sn = $sn;
                    $semesterSubject->class_sn = $val2['class_sn'];
                    $semesterSubject->scored = $val2['scored'];
                    $semesterSubject->scored_mode = $val2['scored_mode'];
                    $semesterSubject->sections = $val2['sections'];
                    $semesterSubject->rate = $val2['rate'];
                    $semesterSubject->tutor_revisable = $val2['tutor_revisable'];
                    $semesterSubject->rank = $val2['rank'];
                    $semesterSubject->enable = $val2['enable'];
                    $semesterSubject->save();
                }

            }
            $this->_helper->json(1);
        }
        else
        $this->_helper->json(0);
    }

    /**
     * 自動加入課程
     *
     */
    function autocourseAction()
    {
        $arr = explode("-",$this->_request->getParam('year_semester'));
        $year = (int)$arr[0];
        $semester = (int)$arr[1];
        $grade = (int)$this->_request->getParam('grade');
        $semester_course_db = new Semester_Model_DbTable_SemesterCourse();
        $semester_course = new Semester_Model_SemesterCourse();
        // 科目名稱
        $score_name = $semester_course->getScoreName();
        $count = $semester_course_db->fetchAll("year=".$year." AND semester=".$semester." AND grade=".$grade)->count();
        if ($count == 0 ) {
            $si = 1;
            foreach($score_name[$grade] as $val) {
                if (!strpos($val['scope'],'-'))
                $si++;
                $srow = $semester_course_db->createRow();
                $srow->year= $year;
                $srow->semester =$semester;
                $srow->grade = $grade;
                $srow->scope = $val['scope'];
                $srow->name = $val['name'];
                $srow->scope_rank = $si;
                $srow->rank = $val['rank'];
                $srow->required = $val['required'];
                $srow->save();
            }
            $this->_helper->json(1);
        }
    }

    /**
     * 新增課程
     * @return unknown_type
     */
    function appendcourseAction()
    {
        $yearSemester = $this->_request->getParam('year_semester');
        $arr = explode("-",$yearSemester);
        $year = $arr[0];
        $semester = $arr[1];
        $grade = $this->_request->getParam('grade');
        $scope = $this->_request->getParam('scope');
        $name = $this->_request->getParam('subject_name');
        $special = $this->_request->getParam('special');
        $semesterCourse = new Semester_Model_SemesterCourse();
        $q = $semesterCourse->getTable()->createQuery('a')
        ->where('year=?', $year)
        ->andWhere('semester=?', $semester)
        ->andWhere('grade=?', $grade)
        ->andWhere('scope LIKE ?',"$scope%");
        $res = $q->fetchOne();
        if (!$res) {
            $q = $semesterCourse->getTable()->createQuery('a')
            ->select('MAX(scope_rank) AS mm')
            ->where('year=?', $year)
            ->andwhere('semester=?', $semester)
            ->andWhere('grade=?', $grade);
            $scopeRank = $q->fetchOne()->mm +1;
            $rank = 1;
        }
        else{
            $scopeRank = $res['scope_rank'];
              $q = $semesterCourse->getTable()->createQuery('a')
            ->select('MAX(rank) AS mm')
            ->where('year=?', $year)
            ->andwhere('semester=?', $semester)
            ->andWhere('grade=?', $grade)
            ->andWhere('scope LIKE ?',"$scope%");
            $rank = $q->fetchOne()->mm+1;
        }

        $arr= array();
        $arr['year'] = $year;
        $arr['semester'] = $semester;
        $arr['name'] = $name;
        $arr['grade'] = $grade;
        $arr['scope'] = $scope;
        $arr['scope_rank'] = $scopeRank;
        $arr['rank'] = $rank;
        $arr['special'] = $special;
        $arr['enable'] = 'Y';
        $arr['required'] = 0;
        $semesterCourse->fromArray($arr);
        $semesterCourse->save();
        $this->_helper->json(true);
    }
}
